package models

import (
	"go-admin/global/orm"
	"go-admin/tools"
	_ "time"
)

type ZxStaff struct {
	Id        int    `json:"id" gorm:"type:int(11) unsigned;primary_key"` //
	Name      string `json:"name" gorm:"type:varchar(50);"`               // 设计者
	Tel       string `json:"tel" gorm:"type:varchar(11);"`                // 电话
	Addr      string `json:"addr" gorm:"type:varchar(200);"`              // 地址
	Note      string `json:"note" gorm:"type:varchar(200);"`              // 备注
	State     int    `json:"state" gorm:"type:int(2);"`                   // 状态
	CreateBy  string `json:"createBy" gorm:"type:varchar(128);"`          // 创建者
	UpdateBy  string `json:"updateBy" gorm:"type:varchar(128);"`          // 更新者
	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`
	BaseModel
}

func (ZxStaff) TableName() string {
	return "zx_staff"
}

// 创建ZxStaff
func (e *ZxStaff) Create() (ZxStaff, error) {
	var doc ZxStaff
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取ZxStaff
func (e *ZxStaff) Get() (ZxStaff, error) {
	var doc ZxStaff
	table := orm.Eloquent.Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	if e.Name != "" {
		table = table.Where("name like ?", "%"+e.Name+"%")
	}

	if e.Tel != "" {
		table = table.Where("tel = ?", e.Tel)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取ZxStaff带分页
func (e *ZxStaff) GetPage(pageSize int, pageIndex int) ([]ZxStaff, int, error) {
	var doc []ZxStaff

	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	if e.Name != "" {
		table = table.Where("name like ?", "%"+e.Name+"%")
	}

	if e.Tel != "" {
		table = table.Where("tel = ?", e.Tel)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新ZxStaff
func (e *ZxStaff) Update(id int) (update ZxStaff, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除ZxStaff
func (e *ZxStaff) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&ZxStaff{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *ZxStaff) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&ZxStaff{}).Error; err != nil {
		return
	}
	Result = true
	return
}
